﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6.ConvertBinaryToHexDirectly
{
    class ConvertBinaryToHexDirectly
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Binary:");
            string binaryNumber = Console.ReadLine();
            string result = HEX(binaryNumber);
            Console.WriteLine(result);
        }
        static string HEX(string binaryNumber)
        {
            int length = binaryNumber.Length;
            StringBuilder result = new StringBuilder();
            if (length % 4 == 0)
            {
                
                for (int i = 0; i < binaryNumber.Length; i += 4)
                {
                    switch (binaryNumber.Substring(i, 4))
                    {
                        case "1010": result.Append("A"); break;
                        case "1011": result.Append("B"); break;
                        case "1100": result.Append("C"); break;
                        case "1101": result.Append("D"); break;
                        case "1110": result.Append("E"); break;
                        case "1111": result.Append("F"); break;
                        case "0000": result.Append("0"); break;
                        case "0001": result.Append("1"); break;
                        case "0010": result.Append("2"); break;
                        case "0011": result.Append("3"); break;
                        case "0100": result.Append("4"); break;
                        case "0101": result.Append("5"); break;
                        case "0110": result.Append("6"); break;
                        case "0111": result.Append("7"); break;
                        case "1000": result.Append("8"); break;
                        case "1001": result.Append("9"); break;

                    }
                }
                return result.ToString();
            }
            else
            {
                char[] temp;
                temp = new char[binaryNumber.Length + 1];
                temp[0] = '0';
                for(int i=1, j=0;i<binaryNumber.Length+1;i++,j++)
                {
                    temp[i] = binaryNumber[j];
                }
                
                string binaryNumber1 = new string(temp);

                Console.WriteLine("= : {0}",binaryNumber1);
                return HEX(binaryNumber1);
            }
        }
    }
}
